<?php

class ZFKiller_Db_Table_Rowset extends Zend_Db_Table_Rowset_Abstract {

	/**
	 *
	 * @param type $key
	 * @param type $value
	 * @return Zend_Db_Table_Row_Abstract|null
	 */
	public function find($key, $value) {
		foreach ($this as $row) {
			if ($row->$key === $value) {
				return $row;
			}
		}
		return null;
	}

	public function toArrayAssoc($key, $value = null, $valueExpression = null) {
		$data = $this->toArray();
		$return = array();

		if (is_array($value)) {
			$iCount = count($value);
		}

		foreach ($data as $row) {
            if (null !== $valueExpression) {
                $add = strtr($valueExpression, $row);
            }
			elseif (null === $value) {
				$add = $row;
			}
			elseif (is_array($value)) {
				$add = array();
				for ($i = 0; $i < $iCount; $i++) {
					$add[$value[$i]] = $row[$value[$i]];
				}
			}
			else {
				$add = $row[$value];
			}

			if (null === $key) {
				$return[] = $add;
			}
			else {
				$return[$row[$key]] = $add;
			}
		}
		return $return;
	}

}